{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ROC\n",
    "* Receiver Operation Characteristic Curve\n",
    "* 描述TPR和FPR之间的关系\n",
    "\n",
    "### $TPR(True\\ Positive\\ Rate)=Recall=\\frac{TP}{TP+FN}$\n",
    "预测为1, 并且预测对了的数量占真实值为1的比例\n",
    "### $FPR(False\\ Positive\\ Rate)=\\frac{FP}{TN+FP}$\n",
    "预测为1, 可惜预测错了的数量占真实值为0的比例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "\n",
    "digits = datasets.load_digits()\n",
    "X = digits.data\n",
    "y = digits.target.copy()\n",
    "\n",
    "y[digits.target==9] = 1\n",
    "y[digits.target!=9] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train,X_test, y_train,y_test = train_test_split(X,y, random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "log_reg = LogisticRegression()\n",
    "log_reg.fit(X_train, y_train)\n",
    "decision_scores = log_reg.decision_function(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from c32_metrics import TPR,FPR\n",
    "\n",
    "fprs = []\n",
    "tprs = []\n",
    "thresholds = np.arange(np.min(decision_scores), np.max(decision_scores), 0.1)\n",
    "for threshold in thresholds:\n",
    "    y_predict = np.array(decision_scores>=threshold, dtype='int')\n",
    "    fprs.append(FPR(y_test, y_predict))\n",
    "    tprs.append(TPR(y_test, y_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAEMNJREFUeJzt3X2QXXV9x/H3NxsSkIeA7PLQPLixBssKtKE7lNa2okEawkwydqyTWMbqUDPaYv/QdgalpQ5OZ1qtOnWIDxl1UGcE0Znqjkap1VCsNUIcFEhocI1g1kSSUAxgCHnYb/+4F+dms5t7dvfu3r2/fb9m7sx5+OXc72/v7ofD75zzu5GZSJLKMqfdBUiSWs9wl6QCGe6SVCDDXZIKZLhLUoEMd0kqkOEuSQUy3CWpQIa7JBVobrveuLu7O3t7e9v19pLUkX7wgx/sz8yeZu3aFu69vb1s3bq1XW8vSR0pIh6v0s5hGUkqkOEuSQUy3CWpQIa7JBXIcJekAjUN94j4dETsjYiHx9gfEfGRiBiMiAcj4vLWlylJGo8qZ+63AytPsv9aYFn9tR742OTLkiRNRtP73DPz3ojoPUmTNcBns/Z9fVsi4uyIuDAz97SoRk1AZnLHfbv4xYHn2l2KpBFWXHw+v7347Cl9j1Y8xLQQ2NWwPlTfdkK4R8R6amf3LFmypAVvrbFs3/M07/n3hwCIaHMxko5z3lmndkS4jxYdo37rdmZuBDYC9Pf3+83cU+hrD+6ha05w33tWcO4Z89tdjqRp1oq7ZYaAxQ3ri4DdLTiuJigz+dpDe/iD3zzXYJdmqVaE+wDwpvpdM1cCBxxvn3rDw8nRY8Ojvh76+QEef/Igqy69sN1lSmqTpsMyEXEHcBXQHRFDwD8CpwBk5seBTcAqYBA4CLxlqopVzYHnjrDig//F/mefH7NN15zgT15xwTRWJWkmqXK3zLom+xP465ZVpKa+uf0J9j/7PH/5h0tZcNopo7ZZdv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EXBQR342ILRGxctqqmxpV+vxe4PqIGKI23ck7pqe0thnv3/u4VJpbpk1aNo98B6ncn4i4HugHXjWlFU29k/Y5IuZQ++rGN09XQdOgyuc8l9rQzFXU/u/sOxFxSWb+coprmypV+rwOuD0zPxgRv09tMsJLMnN46striynNr5l85j4b55Gv0mci4mrgZmB1Zj4/TbVNlWZ9PhO4BLgnIh6jNjY50OEXVav+bn8lM49k5k+BHdTCvlNV6fMNwF0Amfk94FRqc7CUqtLf+0TN5HCfjfPIN+1zfYjiE9SCvdPHYaFJnzPzQGZ2Z2ZvZvZSu86wOjO3tqfclqjyu/1lahfPiYhuasM0O6e1ytaq0uefASsAIuJiauFe8vdxDgBvqt81cyVwIDP3tOzo7b6i3ORq8yrgUWpX2W+ub7uV2h831D78LwKDwH3AS9td8zT0+T+BJ4Af1l8D7a55qvs8ou09dPjdMhU/5wA+BGwHHgLWtrvmaehzH/BdanfS/BC4pt01T7K/dwB7gCPUztJvAN4GvK3hM95Q/3k81Orfa59QlaQCzeRhGUnSBBnuklQgw12SCmS4S1KBDHdJKpDhLkkFMtwlqUCGuyQV6P8BtCHSRpKHg0MAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1ce7e603f28>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(fprs, tprs)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Scikit-learn中的ROC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import roc_curve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGx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OtICq2pQ/9L4N+9/AK4FLgO8D04va/BXwif7rw8AXx133BvT5jcBv91+/eyv0ud/uUuCbwElgZtx1b8DnvB/4HvC7/e2XjbvuDejzceDd/dfTwMPjrnuNff4j4LXADy9y/Abga/RuvHgtcGqU77+ZR+5b8T7yQ/tcVfdW1c/7myfp3chtknX5nAE+BHwE+MVGFrdOuvT5ncCxqnoaoKqe3OAaR61Lnwt4/jmaL+bCGxROlKr6JsvfQPEQ8LnqOQm8JMnlo3r/zRzuI7uP/ATp0udBN9P7L/8kG9rnJFcBu6vqqxtZ2Drq8jlfAVyR5NtJTvYfdTnJuvT5g8BNSebp3e7kPRtT2tis9N/7imzmB2SP7D7yE6Rzf5LcBMwAb1jXitbfsn1O8gJ6j258x0YVtAG6fM7b6U3NXEfv/86+leTKqvrpOte2Xrr0+Qjwmar6pySvo3czwiur6n/Xv7yxWNf82swj9614H/kufSbJm4H3Awer6pcbVNt6GdbnS4ErgW8keZje3OTshC+qdv3dvruqflVVDwEP0gv7SdWlzzcDdwFU1XeAF9G7B0urOv17X63NHO5b8T7yQ/vcn6L4JL1gn/R5WBjS56p6pqp2VNXeqtpLb53hYFXNjafckejyu/0VeovnJNlBb5rm/IZWOVpd+vwI8CaAJK+iF+4tP49zFnh7/6qZa4FnquqJkZ193CvKQ1abbwD+i94q+/v7+26n948beh/+l4BzwH8Crxx3zRvQ538H/gc43f+ZHXfN693nRW2/wYRfLdPxcw7wz8BZ4AfA4XHXvAF9nga+Te9KmtPAn4y75jX29wvAE8Cv6I3SbwbeBbxr4DM+1v/7+MGof6/9hqokNWgzT8tIklbJcJekBhnuktQgw12SGmS4S1KDDHdJapDhLkkNMtwlqUH/B3Y1ff004EIsAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1ce7e622278>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fprs, tprs, thresholds = roc_curve(y_test, decision_scores)\n",
    "plt.plot(fprs, tprs)\n",
    "plt.show()\n",
    "# 我们关注的是ROC曲线下面的面积, 面积越大越好"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## roc_auc_score主要是用来比较两个模型的好坏\n",
    "\n",
    "\n",
    "ROC对不均衡的数据不敏感, 还是PR曲线能反映出不均衡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 求ROC的面积用下面这个\n",
    "from sklearn.metrics import roc_auc_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9830452674897119"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "roc_auc_score(y_test, decision_scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
